package com.atguigu.controller;

import com.atguigu.bean.Department;
import com.atguigu.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DepartmentController {
    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/depts")
    public List<Department> getDepts() {
        return departmentMapper.getDepts();
    }

    @GetMapping("/dept/{id}")
    public Department getDeptById(@PathVariable("id") Integer id) {
        Department dept = departmentMapper.getDeptById(id);
        return dept;
    }

    @RequestMapping("/dept/add")
    public Department addDept(Department dept) {
        if(!StringUtils.isEmpty(dept.getDepartmentName())) {
            departmentMapper.insertDept(dept);
        }
        return dept;
    }

    @RequestMapping("/dept/edit")
    public Department editDept(Department dept) {
        if(!StringUtils.isEmpty(dept.getId()) && !StringUtils.isEmpty(dept.getDepartmentName())) {
            departmentMapper.updateDept(dept);
        }
        return dept;
    }

    @RequestMapping("/dept/delete/{id}")
    public int deleteDept(@PathVariable("id") Integer id) {
        int result = departmentMapper.deleteDept(id);
        return result;
    }
}
